<?php get_main_menu("receptionist") ?>
   <br/>
   <div style="width:90%; margin-left:auto; margin-right:auto;">
    <div class="form_title">

            <?php echo $_REQUEST["action"];?> Order Item <?php echo $row_get_branch_by_id['name']?>
    </div>
    <div class="box"> 
<?php
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "order_item_form") && (isset($_POST["save"]))) {
  $insertSQL = sprintf("INSERT INTO order_item (item_id, color, material_id, type_of_service_id, order_id, created_date,created_by, record_status) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['item_id'], "int"),
                       GetSQLValueString($_POST['color'], "text"),
                       GetSQLValueString($_POST['material_id'], "int"),
                       GetSQLValueString($_POST['type_of_service_id'], "int"),
                       GetSQLValueString($_POST['order_id'], "int"),
					   GetSQLValueString(date("y-m-d"), "int"),
					   GetSQLValueString($loggedin_id, "int"),
                       GetSQLValueString($_POST['record_status'], "text"));

  mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
  $Result1 = mysql_query($insertSQL, $drycleaners_conn) or die(mysql_error());
    if(mysql_affected_rows($drycleaners_conn)>0){
		  
	mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
	$query_get_order_item_last_insert = "SELECT * FROM drycleaners.order_item ORDER BY order_item.id DESC LIMIT 0,1";
	$get_order_item_last_insert = mysql_query($query_get_order_item_last_insert, $drycleaners_conn) or die(mysql_error());
	$row_get_order_item_last_insert = mysql_fetch_assoc($get_order_item_last_insert);
	$totalRows_get_order_item_last_insert = mysql_num_rows($get_order_item_last_insert);

	if (isset($_POST['order_item_service'])) {
				$optionArray = $_POST['order_item_service'];				
					
				for ($i=0; $i<count($optionArray); $i++) {
				$service_data=$optionArray[$i];			
				mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
				$spd_insert=mysql_query("INSERT INTO order_item_service(order_item_id,service_id,created_by, created_date, record_status) 
				VALUES('".$row_get_order_item_last_insert['id']."','$service_data','$loggedin_id','".date("Y-m-d")."','".$_POST['record_status']."')",$drycleaners_conn) or die(mysql_query());	 
				}
				redirect_drycleaners("order_item_view&order_id=".$_REQUEST['order_id']."&back_page=".$_REQUEST['back_page']."","2");
	  }
}

}

mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_item = "SELECT * FROM item";
$item = mysql_query($query_item, $drycleaners_conn) or die(mysql_error());
$row_item = mysql_fetch_assoc($item);
$totalRows_item = mysql_num_rows($item);

mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_type_of_service = "SELECT * FROM type_of_service";
$type_of_service = mysql_query($query_type_of_service, $drycleaners_conn) or die(mysql_error());
$row_type_of_service = mysql_fetch_assoc($type_of_service);
$totalRows_type_of_service = mysql_num_rows($type_of_service);

mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_material = "SELECT * FROM material";
$material = mysql_query($query_material, $drycleaners_conn) or die(mysql_error());
$row_material = mysql_fetch_assoc($material);
$totalRows_material = mysql_num_rows($material);


$colname_order_item_by_id = "-1";
if (isset($_GET['id'])) {
  $colname_order_item_by_id = $_GET['id'];
}
mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_order_item_by_id = sprintf("SELECT * FROM order_item WHERE id = %s", GetSQLValueString($colname_order_item_by_id, "int"));
$order_item_by_id = mysql_query($query_order_item_by_id, $drycleaners_conn) or die(mysql_error());
$row_order_item_by_id = mysql_fetch_assoc($order_item_by_id);
$totalRows_order_item_by_id = mysql_num_rows($order_item_by_id);

$colname_order_item_service = "-1";
if (isset($_GET['id'])) {
  $colname_order_item_service = $_GET['id'];
}
mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_order_item_service = sprintf("SELECT * FROM order_item_service WHERE order_item_id = %s", GetSQLValueString($colname_order_item_service, "int"));
$order_item_service = mysql_query($query_order_item_service, $drycleaners_conn) or die(mysql_error());
$row_order_item_service = mysql_fetch_assoc($order_item_service);
$totalRows_order_item_service = mysql_num_rows($order_item_service);

mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_service = "SELECT * FROM service";
$service = mysql_query($query_service, $drycleaners_conn) or die(mysql_error());
$row_service = mysql_fetch_assoc($service);
$totalRows_service = mysql_num_rows($service);
?>


<form action="<?php echo $editFormAction; ?>" method="POST" name="order_item_form" id="order_item_form">
  <table align="left">
    <tr valign="baseline">
      <td nowrap="nowrap" align="left">Item</td>
      <td><select name="item_id">
        <?php
do {  
?>
        <option value="<?php echo $row_item['id']?>"<?php if (!(strcmp($row_item['id'], $row_order_item_by_id['item_id']))) {echo "selected=\"selected\"";} ?>><?php echo $row_item['name']?></option>
        <?php
} while ($row_item = mysql_fetch_assoc($item));
  $rows = mysql_num_rows($item);
  if($rows > 0) {
      mysql_data_seek($item, 0);
	  $row_item = mysql_fetch_assoc($item);
  }
?>
      </select></td>
    </tr>
    <tr>
          <td nowrap="nowrap" align="left">material</td>
      <td><select name="material_id">
        <?php
do {  
?>
        <option value="<?php echo $row_material['id']?>"<?php if (!(strcmp($row_material['id'], $row_order_material_by_id['material_id']))) {echo "selected=\"selected\"";} ?>><?php echo $row_material['name']?></option>
        <?php
} while ($row_material = mysql_fetch_assoc($material));
  $rows = mysql_num_rows($material);
  if($rows > 0) {
      mysql_data_seek($material, 0);
	  $row_material = mysql_fetch_assoc($material);
  }
?>
      </select></td>
    </tr>
          <td nowrap="nowrap" align="left">type_of_service</td>
      <td><select name="type_of_service_id">
        <?php
do {  
?>
        <option value="<?php echo $row_type_of_service['id']?>"<?php if (!(strcmp($row_type_of_service['id'], $row_order_type_of_service_by_id['type_of_service_id']))) {echo "selected=\"selected\"";} ?>><?php echo $row_type_of_service['name']?></option>
        <?php
} while ($row_type_of_service = mysql_fetch_assoc($type_of_service));
  $rows = mysql_num_rows($type_of_service);
  if($rows > 0) {
      mysql_data_seek($type_of_service, 0);
	  $row_type_of_service = mysql_fetch_assoc($type_of_service);
  }
?>
      </select></td>
    </tr>
    
  
    <tr valign="baseline">
      <td nowrap="nowrap" align="left">Color</td>
      <td><input type="text" name="color" value="<?php echo $row_order_item_by_id['color']; ?>" size="32" /></td>
    </tr>
    </tr>
        <tr valign="baseline">
      <td nowrap="nowrap" align="left">Services</td>
      <td>
      <?php
      	if(!isset($_REQUEST["order_item_edit"])){
        include"includes/order_item/extras.php";
        }
		else{
			include"includes/order_item/extras_edit.php";
			$get_order_item_service = mysql_query("SELECT service.name
  FROM    drycleaners.order_item_service order_item_service
       INNER JOIN
          drycleaners.service service
       ON (order_item_service.service_id = service.id)
	   WHERE order_item_id='".$_REQUEST['id']."'
	   ") or die(mysql_error());
	   $row_get_order_item_service = mysql_fetch_assoc($get_order_item_service);
	    do{ echo $row_get_order_item_service['name']."<br/>";}while($row_get_order_item_service=mysql_fetch_assoc($get_order_item_service));
			}
		?>
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="left">&nbsp;</td>
      <td><?php if(isset($_REQUEST['order_item_edit'])){	
		echo"<input name='update' type='submit' value='UPDATE' id='submit'/>";
		echo" <a href='.?order_item_view= true&order_id=".$_REQUEST['order_id']."&back_page=order_view'><input name='add' type='button' value='Cancel' id='submit'/></a>";
		echo"<input type='hidden' name='MM_update' value='order_item_form' />";	
		}
		else{
			echo"<input name='save' type='submit' value='SAVE' id='submit'/>";
			echo" <a href='.?order_item_view= true&order_id=".$_REQUEST['order_id']."&back_page=order_view'><input name='add' type='button' value='Cancel' id='submit'/></a>";
			echo"<input type='hidden' name='MM_insert' value='order_item_form' />";	
			}
		?></td>
    </tr>
  </table>
  <input type="hidden" name="order_id" value="<?php echo $_REQUEST["order_id"] ?>" />
  <input type="hidden" name="id" value="<?php echo $row_order_item_by_id['id']; ?>" />
  <input type="hidden" name="record_status" value="active" />
  <input type="hidden" name="MM_insert" value="order_item_form" />
</form>
<?php
mysql_free_result($item);
mysql_free_result($type_of_service);
mysql_free_result($material);
mysql_free_result($order_item_by_id);

mysql_free_result($order_item_service);

mysql_free_result($service);
?>
</div>